<?php


namespace Home\Controller;
use OT\DataDictionary;


/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	//系统首页
    public function index(){
        $category = D('Category')->getTree();
        $this->assign('category',$category);//栏目
        $this->xsqg();//限时抢购
        $this->ishot();//热卖商品        
        $this->meta_title = '商城主页';  
        $this->display();
    }
    //列表页
    public function listlie($order = '`id` DESC', $status = 1, $field = true){
		$case = I('get.case');

		$map = array('status' => $status, 'case' => $case);
		$tt= M('goodshop')->where($map)->select();
		$this->page($map,$order);
		if(empty($tt)){
			$re = M('cateshop')->field('id')->where(array('pid'=>$case))->select();
			$arr=array();
			  foreach ($re as $v) {
              array_push($arr,$v['id']);
            }
            $d = implode(',', $arr);
            $map['case'] =array('in',$d,'and');
			$this->page($map,$order);

		}
		 $res = $this->digui($case);
		 $this->assign('res',$res);//面包屑
		 $this->meta_title = '分类列表';
		 $this->display();

	}



	public function digui($case,&$a=array()){
		$res=M('cateshop')->where(array('id'=>$case))->find();
		if($res){
			    array_unshift($a,$res);
			    $this->digui($res['pid'],$a);
		}
		return $a;
	}
    //详情页
	public function iemt(){
		 $id = I('get.gid'); 
		 $good = M('goodshop')->where('id='.$id)->find();
		 $key = M('attrkey')->where('gid='.$id)->select();
		 $value = M('attrval')->where('gid='.$id)->select();
		 // var_dump($good,$key,$value);

		 $good = $this->bltu($good);
		 $good = $this->lowprice($good);
		 foreach ($key as $k => $v) {
		 	foreach ($value as $kk => $vv) {
		 		if($vv['kid']==$v['id']){
		 			$key[$k]['val'][] = $vv;
		 		}
		 	}
		 }
		 $res = $this->digui($good['case']);
		 $this->assign('good',$good);
		 $this->assign('nicen',$key);
		 $this->assign('res',$res);
		 $this->meta_title = '商品详情';
		 $this->display();
	}
    

    public function brand(){
		 $sou = I('get.sou'); 
		 $good = M('goodshop')->where('id='.$id)->find();
		 $good = $this->bltu($good);
		 $good = $this->lowprice($good);
		 $this->assign('good',$good);
		 $this->meta_title = '品牌馆';
		 $this->display();
	}


	public function price(){
		$map['gid']= I('post.gid');
		$map['attrvid']= implode(',',I('post.attr'));
		$price = M('attrprice')->where($map)->find();
		if($price){
			$data['info'] = $price;
		    $data['msg'] ='success';
		    $data['status'] = 1;
		}else{
			$data['info'] = '';
		    $data['msg'] ='failed';
		    $data['status'] = 0;
		}
		$this->ajaxReturn($data);
		}
		
	

	//搜索
	public function sou(){
		$title = I('name');
		$map['status']  =   array('egt',0);
        if(is_numeric($title)){
            $map['name']=   array(intval($title),array('like','%'.$title.'%'),'_multi'=>true);
        }else{
            $map['name|des']    =   array('like', '%'.(string)$title.'%');
        }
         // session('title',$title);
         // var_dump($title);
        // var_dump($res);
         $this->page($map);
         $this->assign('title',$title);
		 $this->meta_title = '搜素页面';
		 $this->display('listlie');
	}


	public function page($map,$order="ctime",$table="goodshop"){
		$good = M($table); 
        // if (!empty($list)) {
        //   $good  =$list;
        //   }  
         
		$count      = $good->where($map)->count();
    
		$page       = new \Think\Page($count,3);
		    $page->setConfig('prev', '上一页');
            $page->setConfig('next', '下一页');
            $page->setConfig('theme', '%UP_PAGE% %LINK_PAGE% %DOWN_PAGE%');
		    $show  = $page->show();
		     
		$list = $good->where($map)->order($order)->limit($page->firstRow.','.$page->listRows)->select();
		$list = $this->bltu($list);
		$list = $this->lowprice($list);
		// var_dump($list,$map);
		$this->assign('lists',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		
	}
	




}